package com.mbt.mobile.bluetooth.screen;

import java.io.IOException;
import java.util.Vector;

import javax.bluetooth.RemoteDevice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.mbt.mobile.bluetooth.manager.BluetoothConnectionManager;

/**
 * The Class Home.
 */
public class Home extends MIDlet implements CommandListener {

	/** The display. */
	private Display display;

	/** The search command. */
	private Command searchCommand;

	/** The exit command. */
	private Command exitCommand;

	/** The back command. */
	private Command backCommand;

	/** The bluetooth connection manager. */
	private BluetoothConnectionManager bluetoothConnectionManager;

	/** The device list. */
	private Vector deviceList;

	/** The devices. */
	private List devices;

	/** The welcome screen. */
	private WelcomeScreen welcomeScreen;

	/** The device list screen. */
	private DeviceListScreen deviceListScreen;
	
	/** The remote desktop screen. */
	private RemoteDesktopScreen remoteDesktopScreen;

	/**
	 * Instantiates a new home.
	 */
	public Home() {
		display = Display.getDisplay(this);
		searchCommand = new Command("Search", Command.OK, 0);
		exitCommand = new Command("Exit", Command.EXIT, 0);
		backCommand = new Command("Back", Command.EXIT, 0);

		welcomeScreen = new WelcomeScreen();
		welcomeScreen.addCommand(searchCommand);
		welcomeScreen.addCommand(exitCommand);
		welcomeScreen.setCommandListener(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.microedition.midlet.MIDlet#pauseApp()
	 */
	protected void pauseApp() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.microedition.midlet.MIDlet#startApp()
	 */
	protected void startApp() throws MIDletStateChangeException {
		display.setCurrent(welcomeScreen);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.microedition.lcdui.CommandListener#commandAction(javax.microedition
	 * .lcdui.Command, javax.microedition.lcdui.Displayable)
	 */
	public void commandAction(Command c, Displayable arg1) {
		System.gc();

		if (c == searchCommand) {

			bluetoothConnectionManager = new BluetoothConnectionManager();
			while (!bluetoothConnectionManager.isComplete())
				;

			deviceList = bluetoothConnectionManager.getDeviceLIst();

			devices = new List("Select a device", List.IMPLICIT);

			for (int i = 0; i < deviceList.size(); i++) {
				devices.append(bluetoothConnectionManager
						.getDeviceStr((RemoteDevice) deviceList.elementAt(i)),
						null);
			}
			if (!deviceList.isEmpty()) {
				devices.setCommandListener(new CommandListener() {

					public void commandAction(Command arg0, Displayable arg1) {

						bluetoothConnectionManager
								.startServiceSearch((RemoteDevice) deviceList
										.elementAt(devices.getSelectedIndex()));
						while (!bluetoothConnectionManager.isConnected())
							;
						
						System.gc();
						try {
							Thread.sleep(100);
						} catch (InterruptedException e2) {
						}
						Image img = null;
						try {
							img = bluetoothConnectionManager.getImage();
						} catch (Exception e1) {
							//throw new RuntimeException(e1.getMessage());
						}
						if(img == null)
						{
							System.gc();
							try {
								img =Image.createImage("/welcome.jpg");
							} catch (IOException e) {
							}
						}
						System.gc();
						try {
							Thread.sleep(500);
						} catch (InterruptedException e2) {
							e2.printStackTrace();
						}
						remoteDesktopScreen = new RemoteDesktopScreen(img,
								bluetoothConnectionManager);
						display.setCurrent(remoteDesktopScreen);

					}
				});

				display.setCurrent(devices);
			} else {
				deviceListScreen = new DeviceListScreen();
				deviceListScreen.addCommand(backCommand);
				deviceListScreen.setCommandListener(this);
				display.setCurrent(deviceListScreen);
			}
		}
		else if (c == backCommand) {
			display.setCurrent(welcomeScreen);
		} else if (c == exitCommand) {
			try {
				destroyApp(false);
				notifyDestroyed();
			} catch (Exception e) {
			}
		}

	}

	/* (non-Javadoc)
	 * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
	 */
	protected void destroyApp(boolean flag) throws MIDletStateChangeException {

	}

}
